package com.zhangjianling.premp.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.UUID;

public class FileUpLoad {
	public static String getPath(MultipartFile file) {
		try {
			Properties properties = new Properties();
			InputStream in = FileUpLoad.class.getClassLoader()
					.getResourceAsStream("config.properties");
			properties.load(in);
			String basePath = (String) properties.get("uploadImg");
			// 获得文件名
			String fileName = file.getOriginalFilename();
			// String basePath = "/Users/zhangjl/Applications/upload/img/";
			String dataString = new SimpleDateFormat("yyyyMMdd").format(new Date());
			String newFileName = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));
			File newFile = FileUtil.createFile(basePath, dataString + "/"
					+ newFileName);
			file.transferTo(newFile);
			ImageCompression img = new ImageCompression();
			img.imgCompress(basePath+dataString + "/" + newFileName);
			return dataString + "/" + newFileName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
